//
//  RegisterController.m
//  gdemm
//
//  Created by 乔明超 on 16/9/22.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "RegisterController.h"
#import "DepartTreeView.h"
#import "Utils.h"
#import "DepartTrees.h"
#import "Constants.h"
#import "gdsm3.h"
#import "DeviceInfo.h"
#import "Masonry.h"
#import "ToastManager.h"
#import "ASIHTTPRequest.h"
#import "HttpUtil.h"
#import "ApiSupport.h"
#import "regex.h"
@interface RegisterController ()<DepartDelegate,UITextFieldDelegate,ASIHTTPRequestDelegate>
@property (strong,nonatomic) UITextField    *mTvUserName;
@property (strong,nonatomic) UITextField    *mTvTrueName;
@property (strong,nonatomic) UITextField    *mTvPassword;
@property (strong,nonatomic) UITextField    *mTvDepart;
@property (strong,nonatomic) UIButton       *mBtnRegister;
@property (strong,nonatomic) DepartTreeView *departTreeView;
@property(nonatomic,strong)UIView *horizontalLineUserName;
@property(nonatomic,strong)UIView *horizontalLineTrueName;
@property(nonatomic,strong)UIView *horizontalLinePassWd;
@property(nonatomic,strong)UIView *horizontalLineDepart;
@property(nonatomic,strong)DepartTrees * depart;
@end

@implementation RegisterController


- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selectDepart:) name:@"selectDepartment" object:nil];
    
    //设置navigationBar样式
    [self setStyleNavigationBar];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = NSLocalizedString(@"str_User_regist", @"用户注册");
    self.navigationController.navigationBarHidden = NO;
    NSInteger titleFont;
    if ([DeviceTypeJudge isPadDeviceType]) {
        titleFont = 20;
    }else{
        titleFont = 16;
    }
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xffffff],NSFontAttributeName:[UIFont systemFontOfSize:titleFont]};;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(dismissViewController) image:@"navigationBar_back" highImage:@"navigationBar_back"];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    //初始化resiterView
    [self setupRegisterView];
    
}
//设置navigationBar样式
- (void)setStyleNavigationBar{
    NSDictionary *textAtt = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
    [[UINavigationBar appearance]setTitleTextAttributes:textAtt];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:[Utils colorWithKey:@"registerNavBarBgColor"]];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)setupRegisterView{
    //设置背景色
    self.view.backgroundColor = [UIColor whiteColor];
    
    //初始化注册界面
    if ([DeviceTypeJudge isPadDeviceType]) {
        [self setupIPadRegisterView];
    }else{
        
        [self setupIPhoneRegisterView];
    }
}


//iPad UI 适配
- (void)setupIPadRegisterView{

    //线条
    UIView *horizontalLineUserName = [[UIView alloc]init];
    self.horizontalLineUserName = horizontalLineUserName;
    horizontalLineUserName.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //用户名输入框
    self.mTvUserName = [[UITextField alloc]init];
    self.mTvUserName.delegate = self;
    self.mTvUserName.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_user_name", @"用户名") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvUserName.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvUserName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvUserName.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvUserName.font = [UIFont systemFontOfSize:18];
    self.mTvUserName.leftViewMode =  UITextFieldViewModeAlways;
    UIImageView *imageUser = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
    imageUser.image = [UIImage imageNamed:@"logon_icon_04"];
    UIView * userNamePadding = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 46, 22)];
    [userNamePadding addSubview:imageUser];
    self.mTvUserName.leftView = userNamePadding;
    [self.view addSubview:self.mTvUserName];
    [self.mTvUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(120);
        make.width.mas_equalTo(600);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(22);
    }];
    [self.view addSubview:horizontalLineUserName];
    [horizontalLineUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvUserName.mas_bottom).offset(27);
        make.width.mas_equalTo(600);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    
    //线条
    UIView *horizontalLineTrueName = [[UIView alloc]init];
    self.horizontalLineTrueName = horizontalLineTrueName;
    horizontalLineTrueName.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //真实姓名输入框
    self.mTvTrueName = [[UITextField alloc]init];
    self.mTvTrueName.delegate = self;
    self.mTvTrueName.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_true_name", @"真实姓名") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvTrueName.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvTrueName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvTrueName.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvTrueName.font = [UIFont systemFontOfSize:18];
    self.mTvTrueName.leftViewMode =  UITextFieldViewModeAlways;
    
    UIImageView *imageTrueName = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
    imageTrueName.image = [UIImage imageNamed:@"logon_icon_05"];
    self.mTvTrueName.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 46, 22)];
    [self.mTvTrueName.leftView addSubview:imageTrueName];
    [self.view addSubview:self.mTvTrueName];
    [self.mTvTrueName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvUserName.mas_bottom).offset(58);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(22);
    }];
    [self.view addSubview:horizontalLineTrueName];
    [horizontalLineTrueName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvTrueName.mas_bottom).offset(27);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(1);
    }];
    
    //线条
    UIView *horizontalLinePassWd = [[UIView alloc]init];
    self.horizontalLinePassWd = horizontalLinePassWd;
    horizontalLinePassWd.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //密码输入框
    self.mTvPassword = [[UITextField alloc]init];
    self.mTvPassword.delegate = self;
    self.mTvPassword.secureTextEntry = true;
    self.mTvPassword.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_enter_eight_password", @"请输入至少8位密码") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvPassword.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvPassword.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvPassword.font = [UIFont systemFontOfSize:18];
    
    self.mTvPassword.leftViewMode =  UITextFieldViewModeAlways;
    UIImageView *imagePassWd = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
    imagePassWd.image = [UIImage imageNamed:@"logon_icon_06"];
    self.mTvPassword.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 46, 22)];
    [self.mTvPassword.leftView addSubview:imagePassWd];
    
    self.mTvPassword.rightViewMode =  UITextFieldViewModeAlways;
    UIButton *btnSecureText = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 32, 18)];
    [btnSecureText setBackgroundImage:[UIImage imageNamed:@"logon_icon_08"] forState:UIControlStateNormal];
    [btnSecureText setBackgroundImage:[UIImage imageNamed:@"logon_icon_08_pre"] forState:UIControlStateSelected];
    [btnSecureText addTarget:self action:@selector(secureTextBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.mTvPassword.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 32, 18)];
    [self.mTvPassword.rightView addSubview:btnSecureText];
    
    [self.view addSubview:self.mTvPassword];
    [self.mTvPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvTrueName.mas_bottom).offset(58);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(22);
    }];
    [self.view addSubview:horizontalLinePassWd];
    [horizontalLinePassWd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvPassword.mas_bottom).offset(27);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(1);
    }];
    
    
    //线条
    UIView *horizontalLineDepart = [[UIView alloc]init];
    self.horizontalLineDepart = horizontalLineDepart;
    horizontalLineDepart.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //所属部门
    self.mTvDepart = [[UITextField alloc]init];
    self.mTvDepart.delegate = self;
    self.mTvDepart.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_subordinate_departments", @"所属部门") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvDepart.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvDepart.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvDepart.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvDepart.font = [UIFont systemFontOfSize:18];
    self.mTvDepart.leftViewMode =  UITextFieldViewModeAlways;
    
    UIImageView *imageDepart = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
    imageDepart.image = [UIImage imageNamed:@"logon_icon_07"];
    self.mTvDepart.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 46, 22)];
    [self.mTvDepart.leftView addSubview:imageDepart];
    [self.view addSubview:self.mTvDepart];
    [self.mTvDepart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvPassword.mas_bottom).offset(58);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(22);
    }];
    [self.view addSubview:horizontalLineDepart];
    [horizontalLineDepart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvDepart.mas_bottom).offset(27);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(600);
        make.height.mas_equalTo(1);
    }];
    
    UIView *maskView = [[UIView alloc]init];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPresentDepartTreeHandler)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [maskView addGestureRecognizer:tap];
    [self.mTvDepart addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    self.mBtnRegister = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.mBtnRegister setTitle:NSLocalizedString(@"str_ok", "确定") forState:UIControlStateNormal];
    self.mBtnRegister.layer.cornerRadius = 6;
    [self.mBtnRegister setBackgroundColor:[Utils colorWithKey:@"registerNavBarBgColor"]];
    [self.mBtnRegister setTitleColor:[Utils UIColorFromHex:0Xffffff] forState:UIControlStateNormal];
    self.mBtnRegister.titleLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:self.mBtnRegister];
    [self.mBtnRegister addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    [self.mBtnRegister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvDepart.mas_bottom).offset(180);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(454);
        make.height.mas_equalTo(48);
    }];
}

//iPhone UI 适配
- (void)setupIPhoneRegisterView{
    //线条
    UIView *horizontalLineUserName = [[UIView alloc]init];
    self.horizontalLineUserName = horizontalLineUserName;
    horizontalLineUserName.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //用户名输入框
    self.mTvUserName = [[UITextField alloc]init];
    self.mTvUserName.delegate = self;
    self.mTvUserName.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_user_name", @"用户名") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvUserName.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvUserName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvUserName.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvUserName.font = [UIFont systemFontOfSize:14];
    self.mTvUserName.leftViewMode =  UITextFieldViewModeAlways;
    UIImageView *imageUser = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    imageUser.image = [Utils getImageByDevce:@"logon_icon_04"];
    UIView * userNamePadding = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, 15)];
    [userNamePadding addSubview:imageUser];
    self.mTvUserName.leftView = userNamePadding;
    [self.view addSubview:self.mTvUserName];
    [self.mTvUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(66*KHeightScale);
        make.width.mas_equalTo(314*KWidthScale);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(15);
    }];
    [self.view addSubview:horizontalLineUserName];
    [horizontalLineUserName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvUserName.mas_bottom).offset(20);
        make.width.mas_equalTo(314*KWidthScale);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    
    //线条
    UIView *horizontalLineTrueName = [[UIView alloc]init];
    self.horizontalLineTrueName = horizontalLineTrueName;
    horizontalLineTrueName.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //真实姓名输入框
    self.mTvTrueName = [[UITextField alloc]init];
    self.mTvTrueName.delegate = self;
    self.mTvTrueName.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_true_name", @"真实姓名") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvTrueName.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvTrueName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvTrueName.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvTrueName.font = [UIFont systemFontOfSize:14];
    self.mTvTrueName.leftViewMode =  UITextFieldViewModeAlways;
    
    UIImageView *imageTrueName = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    imageTrueName.image = [Utils getImageByDevce:@"logon_icon_05"];
    self.mTvTrueName.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, 15)];
    [self.mTvTrueName.leftView addSubview:imageTrueName];
    [self.view addSubview:self.mTvTrueName];
    [self.mTvTrueName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.horizontalLineUserName.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(314*KWidthScale);
        make.height.mas_equalTo(15);
    }];
    [self.view addSubview:horizontalLineTrueName];
    [horizontalLineTrueName mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.mas_equalTo(self.mTvTrueName.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(314*KWidthScale);
        make.height.mas_equalTo(1);
    }];
    
    
    //线条
    UIView *horizontalLinePassWd = [[UIView alloc]init];
    self.horizontalLinePassWd = horizontalLinePassWd;
    horizontalLinePassWd.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //密码输入框
    self.mTvPassword = [[UITextField alloc]init];
    self.mTvPassword.delegate = self;
    self.mTvPassword.secureTextEntry = true;
    self.mTvPassword.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_enter_eight_password", @"请输入至少8位密码") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvPassword.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvPassword.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvPassword.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvPassword.font = [UIFont systemFontOfSize:14];
    
    self.mTvPassword.leftViewMode =  UITextFieldViewModeAlways;
    UIImageView *imagePassWd = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    imagePassWd.image = [Utils getImageByDevce:@"logon_icon_06"];
    self.mTvPassword.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, 15)];
    [self.mTvPassword.leftView addSubview:imagePassWd];
    
    self.mTvPassword.rightViewMode =  UITextFieldViewModeAlways;
    UIButton *btnSecureText = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 18,18)];
    [btnSecureText setBackgroundImage:[Utils getImageByDevce:@"logon_icon_08"] forState:UIControlStateNormal];
    [btnSecureText setBackgroundImage:[Utils getImageByDevce:@"logon_icon_08_pre"] forState:UIControlStateSelected];
    [btnSecureText addTarget:self action:@selector(secureTextBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.mTvPassword.rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 18,18)];
    [self.mTvPassword.rightView addSubview:btnSecureText];
    
    [self.view addSubview:self.mTvPassword];
    [self.mTvPassword mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.horizontalLineTrueName.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(314*KWidthScale);
        make.height.mas_equalTo(15);
    }];
    [self.view addSubview:horizontalLinePassWd];
    [horizontalLinePassWd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvPassword.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(314*KWidthScale);
        make.height.mas_equalTo(1);
    }];
    
    
    //线条
    UIView *horizontalLineDepart = [[UIView alloc]init];
    self.horizontalLineDepart = horizontalLineDepart;
    horizontalLineDepart.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    //所属部门
    self.mTvDepart = [[UITextField alloc]init];
    self.mTvDepart.delegate = self;
    self.mTvDepart.attributedPlaceholder = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"str_subordinate_departments", @"所属部门") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[Utils UIColorFromHex:0Xcccccc]}];
    self.mTvDepart.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.mTvDepart.autocorrectionType = UITextAutocorrectionTypeNo;
    self.mTvDepart.textColor = [Utils UIColorFromHex:0X323232];
    self.mTvDepart.font = [UIFont systemFontOfSize:14];
    self.mTvDepart.leftViewMode =  UITextFieldViewModeAlways;
    
    UIImageView *imageDepart = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    imageDepart.image = [Utils getImageByDevce:@"logon_icon_07"];
    self.mTvDepart.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, 15)];
    [self.mTvDepart.leftView addSubview:imageDepart];
    [self.view addSubview:self.mTvDepart];
    [self.mTvDepart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.horizontalLinePassWd.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(314*KWidthScale);
        make.height.mas_equalTo(15);
    }];
    [self.view addSubview:horizontalLineDepart];
    [horizontalLineDepart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvDepart.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(314*KWidthScale);
        make.height.mas_equalTo(1);
    }];
    
    UIView *maskView = [[UIView alloc]init];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapPresentDepartTreeHandler)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [maskView addGestureRecognizer:tap];
    [self.mTvDepart addSubview:maskView];
    [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    self.mBtnRegister = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.mBtnRegister setTitle:NSLocalizedString(@"str_ok", "确定") forState:UIControlStateNormal];
    self.mBtnRegister.layer.cornerRadius = 3;
    [self.mBtnRegister setBackgroundColor:[Utils colorWithKey:@"registerNavBarBgColor"]];
    [self.mBtnRegister setTitleColor:[Utils UIColorFromHex:0Xffffff] forState:UIControlStateNormal];
    self.mBtnRegister.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.mBtnRegister];
    [self.mBtnRegister addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    [self.mBtnRegister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mTvDepart.mas_bottom).offset(129*KHeightScale);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(220*KWidthScale);
        make.height.mas_equalTo(35*KHeightScale);
    }];
}
- (void)dismissViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 事件响应
- (void)secureTextBtn:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.mTvPassword.secureTextEntry = !sender.selected;
}

- (void)tapPresentDepartTreeHandler{
    [self.view endEditing:YES];
    UIWindow *keyv=[[UIApplication sharedApplication] keyWindow];
    self.departTreeView = [[DepartTreeView alloc]init];
    self.departTreeView.delegate = self;
    self.departTreeView.frame = [UIScreen mainScreen].bounds;
    [keyv addSubview:self.departTreeView];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 实现通知的方法
- (void)selectDepart:(NSNotification *)notify{
    NSDictionary * dict =  notify.userInfo;
    self.depart = [DepartTrees departTreesWithDictionary:dict];
    self.mTvDepart.text = [dict valueForKey:@"depName"];
    [self.departTreeView removeFromSuperview];
}
#pragma mark DepartDelegate代理协议的实现
-(void)removeDepartView{
    [self.departTreeView removeFromSuperview];
}

#pragma mark -输入框的代理方法
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.mTvUserName) {
        self.horizontalLineUserName.backgroundColor = [Utils colorWithKey:@"registerHorizontalLine"];
    }else if(textField == self.mTvPassword){
        self.horizontalLinePassWd.backgroundColor = [Utils colorWithKey:@"registerHorizontalLine"];
    }else if(textField == self.mTvTrueName){
        self.horizontalLineTrueName.backgroundColor = [Utils colorWithKey:@"registerHorizontalLine"];
    }else if(textField == self.mTvDepart){
        self.horizontalLineDepart.backgroundColor = [Utils colorWithKey:@"registerHorizontalLine"];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    [self.view endEditing:YES];
    if (textField == self.mTvUserName) {
        self.horizontalLineUserName.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    }else if(textField == self.mTvPassword){
        self.horizontalLinePassWd.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    }else if(textField == self.mTvTrueName){
        self.horizontalLineTrueName.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    }else if(textField == self.mTvDepart){
        self.horizontalLineDepart.backgroundColor = [Utils UIColorFromHex:0XE6E6E6];
    }
    
}
-(void)regist{
    NSString *userName = self.mTvUserName.text;
    NSString *realName = self.mTvTrueName.text;
    NSString *passwd = self.mTvPassword.text;
    NSString *part = self.depart.depGUID;
    if ([StringUtil isEmpty:userName]) {
        [self.view makeToast:NSLocalizedString(@"str_emptyUserName", @"用户名不允许为空")];
    } else if(![regex isCorrectName:userName]){
        [self.view makeToast:NSLocalizedString(@"str_UserNameIsNotCorrect",@"用户名必须小于20字符，由数字、字母、下划线、点组成")];
    }else if([StringUtil isEmpty:realName]){
        [self.view makeToast:NSLocalizedString(@"str_emptyTrueName",@"真实姓名不允许为空")];
    } else if([StringUtil isEmpty:passwd]){
        [self.view makeToast:NSLocalizedString(@"str_emptyPswd",@"密码不许为空")];
    } else if(![regex isCorrectPwd:passwd]){
        [self.view makeToast:NSLocalizedString(@"str_PasswordIsNotCorrect", @"密码必须是8-16字符，由数字、字母、下划线、点组成")];
    } else if([StringUtil isEmpty:part]){
        [self.view makeToast:NSLocalizedString(@"str_emptyDepartment", @"部门不许为空")];
    }else {
        [[MBProgressHUD HUDForView:self.view] show:YES];
        const char *str = [passwd UTF8String];
        long len = strlen(str);
        unsigned const  char out_bf[65];
        sm3_buffer((void *)str, (int)len, (void *)out_bf, 32);
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setValue:userName forKey:@"username"];
        [dict setValue:realName forKey:@"trueName"];
        [dict setValue:[NSString stringWithUTF8String:(void *)out_bf] forKey:@"password"];
        [dict setValue:part forKey:@"depGUID"];
        [dict setValue:[DeviceInfo getIpAddress] forKey:@"IP"];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
            NSError *error;
            NSData *data = [HttpUtil httpRequestSyncWithUrl:[ApiSupport getUserRegisterUrl] andParams:dict];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
            [self parseResponse:dict];
        });
        
    }
    
}


/**
 用户注册返回的数据信息

 @param dict 返回的数据信息
 */
-(void) parseResponse:(NSDictionary *)dict{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[MBProgressHUD HUDForView:self.view] show:YES];
        
        NSString *result = [dict objectForKey:@"result"];
        if([result isEqualToString:@"success"]){
            BOOL needApprove = false;
            NSString *regState = [dict objectForKey:@"regState"];
            if ([regState intValue] == 1) {
                needApprove = true;
            }
            if (!needApprove) {
                NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:self.mTvUserName.text,@"userName",self.mTvPassword.text,@"passWord" ,nil];
                NSNotification *notification = [[NSNotification alloc]initWithName:NF_REG_SICCESS object:nil userInfo:dict];
                [[NSNotificationCenter defaultCenter]postNotification:notification];
                
                [[UIApplication sharedApplication].keyWindow makeToast:NSLocalizedString(@"str_registerSuccessPassed",@"注册成功")];
                
                [self.navigationController popViewControllerAnimated:YES];
                
                
            } else {
                [self.view makeToast:NSLocalizedString(@"str_registerSuccess",@"注册成功，需要管理员审核后才可登入")];
                [self performSelector:@selector(popViewController) withObject:nil afterDelay:2];
            }
        } else {
            NSString *errorCode = [dict objectForKey:@"errCode"];
            switch ([errorCode intValue]) {
                case 1:
                    [self.view makeToast:NSLocalizedString(@"str_emptyUserName", @"用户名不允许为空")];
                    break;
                case 2:
                    [self.view makeToast:NSLocalizedString(@"str_emptyPswd",@"密码不许为空")];
                    break;
                case 3:
                    [self.view makeToast:NSLocalizedString(@"str_emptyDepartment", @"部门不许为空")];
                    break;
                case 4:
                    [self.view makeToast:NSLocalizedString(@"str_genGuidFailed", @"生成GUID失败")];
                    break;
                case 5:
                    [self.view makeToast:NSLocalizedString(@"str_userAlreadyExist", @"用户已存在")];
                    break;
                case 6:
                    [self.view makeToast:NSLocalizedString(@"str_depNotFound", @"部门不存在")];
                    break;
                case 7:
                    [self.view makeToast:NSLocalizedString(@"str_sqlError",@"SQL语句错误")];
                    break;
                case 8:
                    [self.view makeToast:NSLocalizedString(@"str_emptyTrueName",@"真实姓名不允许为空")];
                    break;
                case 9:
    
                    [self.view makeToast:NSLocalizedString(@"str_userRegisterLocked",@"用户注册功能已关闭")];
                    break;
            }
            
        }
        
    });
}
-(void)popViewController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
